** Organize data on empty-nester parents in the middle generation, who have
** their own parents living in their households. By construction, there are no 
** potential child dependents of the middle-generation, so the household shouldn't
** be receiving EITC.  But use variables from fertility history topical module
** to identify number of kids ever born, and calculate a "false EITC" as if the
** parents could use these as EITC-qualifying children.  




*******************
** 1990
*******************

***********
** Wave 3
***********

use  EmptyNesterData1990_w3

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1990_w3
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using FertilityHistoryVars1990_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)



gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1990_w3 
keep if _merge==3
drop _merge


gen panel=1990
save OrganizedMidGenEmptyNestData1990_w3, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn 
duplicates drop

duplicates report suid entry srefmon



save MaxMidGenEmptyNestEITC1990_w3, replace
clear





** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1990_w3

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1990_w3, replace
clear





***********
** Wave 4
***********

use  EmptyNesterData1990_w4

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1990_w4
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using FertilityHistoryVars1990_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1990_w4 
keep if _merge==3
drop _merge


gen panel=1990
save OrganizedMidGenEmptyNestData1990_w4, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report suid entry srefmon

save MaxMidGenEmptyNestEITC1990_w4, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1990_w4

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1990_w4, replace
clear




*******************
** 1991: 3, 7
*******************

***********
** Wave 3
***********

use  EmptyNesterData1991_w3

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1991_w3
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using FertilityHistoryVars1991_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1991_w3 
keep if _merge==3
drop _merge


gen panel=1991
save OrganizedMidGenEmptyNestData1991_w3, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report suid entry srefmon

save MaxMidGenEmptyNestEITC1991_w3, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1991_w3

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1991_w3, replace
clear




***********
** Wave 7
***********

use  EmptyNesterData1991_w7

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1991_w7
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using FertilityHistoryVars1991_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1991_w7 
keep if _merge==3
drop _merge


gen panel=1991
save OrganizedMidGenEmptyNestData1991_w7, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report suid entry srefmon

save MaxMidGenEmptyNestEITC1991_w7, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  T
use OrganizedMidGenEmptyNestData1991_w7

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1991_w7, replace
clear




*******************
** 1992: 4, 6, 7
*******************

***********
** Wave 4
***********

use  EmptyNesterData1992_w4

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1992_w4
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using FertilityHistoryVars1992_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1992_w4 
keep if _merge==3
drop _merge


gen panel=1992
save OrganizedMidGenEmptyNestData1992_w4, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report suid entry srefmon

save MaxMidGenEmptyNestEITC1992_w4, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1992_w4

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1992_w4, replace
clear




***********
** Wave 6
***********

use  EmptyNesterData1992_w6

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1992_w6
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using FertilityHistoryVars1992_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1992_w6 
keep if _merge==3
drop _merge


gen panel=1992
save OrganizedMidGenEmptyNestData1992_w6, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report suid entry srefmon

save MaxMidGenEmptyNestEITC1992_w6, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1992_w6

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1992_w6, replace
clear





*******************
** 1993
*******************

***********
** Wave 3
***********

use  EmptyNesterData1993_w3

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1993_w3
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using FertilityHistoryVars1993_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1993_w3 
keep if _merge==3
drop _merge


gen panel=1993
save OrganizedMidGenEmptyNestData1993_w3, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report suid entry srefmon

save MaxMidGenEmptyNestEITC1993_w3, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1993_w3

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1993_w3, replace
clear




***********
** Wave 7
***********

use  EmptyNesterData1993_w7

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 suid addid pnum srefmon using KidCount1993_w7
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables--note that they come from wave 2,
** so this will not be a perfect merge.  Because the fertility history variables
** are so important for what you are doing, only keep people with a merge to 
** that data
tostring suid, replace
merge m:1 suid entry pnum using /FertilityHistoryVars1993_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids ever born from fertility history 
** module as the number of EITC-eligible kids
gen EITC_calc_year = (1900+rhcalyr)-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do




rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort suid entry srefmon
by suid entry srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by suid entry srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by suid entry srefmon: egen N_ENsWithCollege=total(temp_college)
by suid entry srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by suid entry srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort suid entry srefmon
by suid entry srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 suid entry pnum using WellBeingVars1993_w7 
keep if _merge==3
drop _merge


gen panel=1993
save OrganizedMidGenEmptyNestData1993_w7, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC  xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson FH_N_KidsEverBorn 
sort suid entry srefmon
by suid entry srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by suid entry srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort suid entry srefmon
by suid entry srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report suid entry srefmon

save MaxMidGenEmptyNestEITC1993_w7, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1993_w7

keep panel swave suid entry srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1
duplicates report suid entry srefmon
sort suid entry srefmon
by suid entry srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by suid entry srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by suid entry srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave suid entry srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1993_w7, replace
clear




*******************
** 1996
*******************

***********
** Wave 3
***********

use  EmptyNesterData1996_w3

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount1996_w3
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables
merge m:1 ssuid epppnum using FertilityHistoryVars1996_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids from fertility history module
gen EITC_calc_year = rhcalyr-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do



rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by ssuid srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by ssuid srefmon: egen N_ENsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by ssuid srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars1996_w3 
keep if _merge==3
drop _merge


gen panel=1996
save OrganizedMidGenEmptyNestData1996_w3, replace


** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson FH_N_KidsEverBorn 

sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort ssuid srefmon
by ssuid srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEmptyNestEITC1996_w3, replace
clear




** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData1996_w3

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1

duplicates report ssuid srefmon

sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by ssuid srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave ssuid srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_1996_w3, replace
clear


*******************
** 2001
*******************

***********
** Wave 3
***********

use  EmptyNesterData2001_w3

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2001_w3
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
assert Mod_DepCount==0

** Merge in the fertility history variables
merge m:1 ssuid epppnum using FertilityHistoryVars2001_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids from fertility history module
gen EITC_calc_year = rhcalyr-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do



rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by ssuid srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by ssuid srefmon: egen N_ENsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by ssuid srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2001_w3 
keep if _merge==3
drop _merge


gen panel=2001
save OrganizedMidGenEmptyNestData2001_w3, replace


** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson FH_N_KidsEverBorn 

sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort ssuid srefmon
by ssuid srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEmptyNestEITC2001_w3, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData2001_w3

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1

duplicates report ssuid srefmon

sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by ssuid srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave ssuid srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_2001_w3, replace
clear



*******************
** 2004
*******************

***********
** Wave 3
***********

use  EmptyNesterData2004_w3

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2004_w3
assert _merge~=1
keep if _merge==3
drop _merge



** Merge in the fertility history variables
merge m:1 ssuid epppnum using FertilityHistoryVars2004_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids from fertility history module
gen EITC_calc_year = rhcalyr-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do



rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by ssuid srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by ssuid srefmon: egen N_ENsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by ssuid srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2004_w3 
keep if _merge==3
drop _merge


gen panel=2004
save OrganizedMidGenEmptyNestData2004_w3, replace


** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn

sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort ssuid srefmon
by ssuid srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEmptyNestEITC2004_w3, replace
clear




** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData2004_w3

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1

duplicates report ssuid srefmon

sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by ssuid srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave ssuid srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_2004_w3, replace
clear




*******************
** 2008
*******************

***********
** Wave 4
***********

use  EmptyNesterData2008_w4

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid epppnum srefmon using KidCount2008_w4
assert _merge~=1
keep if _merge==3
drop _merge

** By construction, every parent in this empty-nest parent sample
** should have Mod_DepCount==0. (Making you wonder if you even need to merge
** in the KidCount data...)
**assert Mod_DepCount==0
** NOTE: Assertion fails in some cases, but vast majority of these are "mid-gen"
** people who are not living with their own parents--so they are not in population
** of interest.

** Merge in the fertility history variables
merge m:1 ssuid epppnum using FertilityHistoryVars2008_w2
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids from fertility history module
gen EITC_calc_year = rhcalyr-1
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do



rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White


gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)


sort ssuid srefmon
by ssuid srefmon: egen N_SingleFemaleEN=total(SingleFemaleEN)
by ssuid srefmon: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by ssuid srefmon: egen N_ENsWithCollege=total(temp_college)
by ssuid srefmon: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by ssuid srefmon: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(errp==1 | errp==2)
sort ssuid srefmon
by ssuid srefmon: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid epppnum using WellBeingVars2008_w4 
keep if _merge==3
drop _merge


gen panel=2008
save OrganizedMidGenEmptyNestData2008_w4, replace


** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson FH_N_KidsEverBorn 

sort ssuid srefmon
by ssuid srefmon: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid srefmon: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort ssuid srefmon
by ssuid srefmon: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report ssuid srefmon

save MaxMidGenEmptyNestEITC2008_w4, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData2008_w4

keep panel swave ssuid srefmon r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1

duplicates report ssuid srefmon

sort ssuid srefmon
by ssuid srefmon: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by ssuid srefmon: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by ssuid srefmon: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave ssuid srefmon Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_2008_w4, replace
clear





*******************
** 2014
*******************

***********
** Wave 1
***********

use  EmptyNesterData2014_w1

** Merge in count of how many people in household identified this person as his/her mother
merge 1:1 ssuid pnum monthcode using KidCount2014_w1
assert _merge~=1
keep if _merge==3
drop _merge

** Merge in the fertility history variables
merge m:1 ssuid pnum using FertilityHistoryVars2014_w1
keep if _merge==3
drop _merge

** Run EITC calculator, using number of kids from fertility history module
gen EITC_calc_year = 2013
gen xocah = FH_N_KidsEverBorn
quietly do EITC_Calculator.do

rename Married MidGenEN_Married
rename Employed MidGenEN_Employed
rename HSGrad MidGenEN_HSGrad
rename SomeCollege MidGenEN_SomeCollege
rename CollegeGrad MidGenEN_CollegeGrad
rename White MidGenEN_White

gen SingleFemaleEN = (Female==1 & MidGenEN_Married==0)
gen SingleNoCollegeFemaleEN = (Female==1 & MidGenEN_Married==0 & MidGenEN_SomeCollege==0 & MidGenEN_CollegeGrad==0)
gen temp_college = (MidGenEN_SomeCollege==1 | MidGenEN_CollegeGrad==1)

sort ssuid monthcode
by ssuid monthcode: egen N_SingleFemaleEN=total(SingleFemaleEN)
by ssuid monthcode: egen N_SingleNoCollegeFemaleEN = total(SingleNoCollegeFemaleEN)
by ssuid monthcode: egen N_ENsWithCollege=total(temp_college)
by ssuid monthcode: egen N_CollegeGradENs=total(MidGenEN_CollegeGrad)
by ssuid monthcode: egen N_ENsEmployed=total(MidGenEN_Employed)


gen AnySingleFemaleEN = (N_SingleFemaleEN>0)
gen AnySingleNoCollegeFemaleEN=(N_SingleNoCollegeFemaleEN>0)
gen NoENsWithCollege = (N_ENsWithCollege==0)
gen NoCollegeGradENs = (N_CollegeGradENs==0)
gen AnyENsEmployed = (N_ENsEmployed>0)

drop temp_college


gen RefPerson=(erelrp==1 | erelrp==2)
sort ssuid monthcode
by ssuid monthcode: egen N_RefPerson=total(RefPerson)
gen MidGenHasRefPerson=(N_RefPerson>0)

merge m:1 ssuid pnum monthcode using WellBeingVars2014_w1 
keep if _merge==3
drop _merge

gen panel=2014
save OrganizedMidGenEmptyNestData2014_w1, replace



** Extract information at household-month level about MAXIMUM amount of "false" EITC eligibility
** for which any mid-gen parent is available. 

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC xocah AnySingleFemaleEN AnySingleNoCollegeFemaleEN NoENsWithCollege NoCollegeGradENs AnyENsEmployed MidGenHasRefPerson  FH_N_KidsEverBorn

sort ssuid monthcode
by ssuid monthcode: egen r_MaxEITC_MidGenMax=max(r_MaxEITC_thou)
by ssuid monthcode: egen r_ref_MaxEITC_MidGenMax=max(r_ref_MaxEITC)


** Only keep observations of individuals whose EITC eligibility is equal to the max EITC
** eligibility of anyone in the middle generation  (just a way of keeping one record per HH)
keep if r_MaxEITC_MidGenMax == r_MaxEITC_thou
sort ssuid monthcode
by ssuid monthcode: egen Max_FH_N_Dep=max(FH_N_KidsEverBorn)


** For later construction of a summary stats table, distinguish between cases of 1 EITC-eligible
** child and 2+ EITC-eligible children of the middle-generation parent who is eligible for the largest
** EITC amount
gen xocah_1or2plus=.
replace xocah_1or2plus = 1 if xocah==1
replace xocah_1or2plus = 2 if xocah>1 & xocah~=.

drop xocah* r_MaxEITC_thou r_ref_MaxEITC FH_N_KidsEverBorn
duplicates drop

duplicates report ssuid monthcode

save MaxMidGenEmptyNestEITC2014_w1, replace
clear



** Another way to count EITC resources in the middle generation is to sum up amounts received
** by different mid-gen individuals.  You don't want to double-count EITC eligibility of a husband
** and wife, but you could assume that all women are separate filers and sum up all EITC
** amounts of the different mid-gen women.  
use OrganizedMidGenEmptyNestData2014_w1

keep panel swave ssuid monthcode r_MaxEITC_thou r_ref_MaxEITC Female FH_N_KidsEverBorn
keep if Female==1 

duplicates report ssuid monthcode

sort ssuid monthcode
by ssuid monthcode: egen Sum_MaxEITC_MidGenFemales = sum(r_MaxEITC_thou)
by ssuid monthcode: egen Sum_ref_MaxEITC_MidGenFemales = sum(r_ref_MaxEITC)
by ssuid monthcode: egen N_Potential_EN_Deps = sum(FH_N_KidsEverBorn)


keep panel swave ssuid monthcode Sum_MaxEITC_MidGenFemales Sum_ref_MaxEITC_MidGenFemales N_Potential_EN_Deps
duplicates drop
save SumMidGenEmptyNestEITC_2014_w1, replace
clear



*******************************************************
use OrganizedMidGenEmptyNestData1990_w3
append using OrganizedMidGenEmptyNestData1990_w4
append using OrganizedMidGenEmptyNestData1991_w3
append using OrganizedMidGenEmptyNestData1991_w7

append using OrganizedMidGenEmptyNestData1992_w4
append using OrganizedMidGenEmptyNestData1992_w6

append using OrganizedMidGenEmptyNestData1993_w3
append using OrganizedMidGenEmptyNestData1993_w7

append using OrganizedMidGenEmptyNestData1996_w3
append using OrganizedMidGenEmptyNestData2001_w3
append using OrganizedMidGenEmptyNestData2004_w3
append using OrganizedMidGenEmptyNestData2008_w4

append using OrganizedMidGenEmptyNestData2014_w1

tab panel, missing

** What would you lose by trimming down here to just the cases where there
** is an "oldest generation" in the household?


** Merge in a measure of EITC at the household level, the sum over all of
** the mid-gen mothers in the household.
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1990_w3
drop _merge
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1990_w4, update
drop _merge
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1991_w3, update
drop _merge
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1991_w7, update
drop _merge
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1992_w4, update
drop _merge
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1992_w6, update
drop _merge
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1993_w3, update
drop _merge
merge m:1 panel swave suid entry srefmon using SumMidGenEmptyNestEITC_1993_w7, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEmptyNestEITC_1996_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEmptyNestEITC_2001_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEmptyNestEITC_2004_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using SumMidGenEmptyNestEITC_2008_w4, update
drop _merge
merge m:1 panel swave ssuid monthcode using SumMidGenEmptyNestEITC_2014_w1, update
drop _merge


** Merge in the other measure of EITC at the household level, the max of any mid-gen parent
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1990_w3
drop _merge
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1990_w4, update
drop _merge
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1991_w3, update
drop _merge
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1991_w7, update
drop _merge
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1992_w4, update
drop _merge
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1992_w6, update
drop _merge
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1993_w3, update
drop _merge
merge m:1 panel swave suid entry srefmon using MaxMidGenEmptyNestEITC1993_w7, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEmptyNestEITC1996_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEmptyNestEITC2001_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEmptyNestEITC2004_w3, update
drop _merge
merge m:1 panel swave ssuid srefmon using MaxMidGenEmptyNestEITC2008_w4, update
drop _merge
merge m:1 panel swave ssuid monthcode using MaxMidGenEmptyNestEITC2014_w1, update
drop _merge


** Should drop cases in which tfipsst code does not uniquely identify state, when the 
** group of states includes some with different EITC parameters
drop if (panel==1990 | panel==1991 | panel==1992 | panel==1993) & (tfipsst==61 | tfipsst==62 | tfipsst==63)
drop if panel==1996 & (tfipsst==61 | tfipsst==62)
drop if panel==2001 & (tfipsst==61 | tfipsst==62)
** For 2014, drop any cases with state identifier indicating Puerto Rico or foreign country
drop if panel==2014 & (tfipsst==60 | tfipsst==61)



** Person identifer

egen UniquePersonID_p2014 = group(panel ssuid pnum)
replace UniquePersonID_p2014 = . if panel~=2014

egen UniquePersonID_midyears = group(panel ssuid epppnum )
replace UniquePersonID_midyears = . if (panel==1990 | panel==1991 | panel==1992 | panel==1993 | panel==2014)

egen UniquePersonID_p1993 = group(panel suid entry pnum)
replace UniquePersonID_p1993 = . if (panel==1996 | panel==2001 | panel==2004 | panel==2008 | panel==2014)

gen temp_UniquePersonID = UniquePersonID_midyears
replace temp_UniquePersonID = UniquePersonID_p1993 if (panel==1990 | panel==1991 | panel==1992 | panel==1993)
replace temp_UniquePersonID = UniquePersonID_p2014 if panel==2014

egen UniquePersonID = group(panel temp_UniquePersonID)



** Household identifier
egen later_UniqueHouseholdID = group(panel ssuid )
replace later_UniqueHouseholdID = . if (panel==1990 | panel==1991 | panel==1992 | panel==1993)

egen earlier_UniqueHouseholdID = group(panel suid entry)
replace earlier_UniqueHouseholdID = . if (panel==1996 | panel==2001 | panel==2004 | panel==2008 | panel==2014)

gen temp_UniqueHouseholdID = later_UniqueHouseholdID
replace temp_UniqueHouseholdID = earlier_UniqueHouseholdID if (panel==1990 | panel==1991 | panel==1992 | panel==1993)

egen UniqueHouseholdID = group(panel temp_UniqueHouseholdID)




gen ExcellentVeryGoodHealth = (ehltstat==1 | ehltstat==2)


gen AgeSq = tage*tage


gen r_DebtAmount = DebtAmount*real
gen ln_r_Debt = ln(1+r_DebtAmount)

gen r_AssetAmount = AssetAmount*real
gen r_CheckingAmount = CheckingAmount*real
gen r_SavingsCheckingA = SavingsCheckingAmount*real


gen ln_r_AssetAmount = ln(1+r_AssetAmount)
gen ln_r_CheckingAmount=ln(1+r_CheckingAmount)
gen ln_r_SavingsChecking = ln(1+r_SavingsCheckingA)


** Month-by-month variation is not meaningful for the well-being variables that are asked about longer reference period
** Figure out appropriate way to trim down to one obs per person
duplicates report UniquePersonID swave panel
gen uniform_srefmon = srefmon
replace uniform_srefmon = monthcode if panel==2014
sort panel swave UniquePersonID 
by panel swave UniquePersonID: egen min_srefmon = min(uniform_srefmon)
keep if uniform_srefmon==min_srefmon
drop min_srefmon
duplicates report UniquePersonID panel

** For some analysis, it will be useful to identify earliest observation of a given individual
sort UniquePersonID swave
by UniquePersonID: egen min_swave=min(swave)


** Add in info about state-level unemployment, top tax rate, gdp, etc
recode rhcalyr 90=1992
recode rhcalyr 91=1992
recode rhcalyr 92=1992
recode rhcalyr 93=1993
recode rhcalyr 94=1994
replace rhcalyr = 2013 if panel==2014 & swave==1

merge m:1 tfipsst rhcalyr using OrganizedStateControls
tab _merge
keep if _merge==3
drop _merge

gen r_MaxWelf = MaxWelf*real



** Create variables that will be helpful in identifying samples for health-related
** regressions and samples for debt-related regressions
gen MissingHealthInfo=(ehltstat==. | ehltstat==0)
gen MissingDebtInfo = (AnyDebt==. | AnyCCDebt==. | ln_r_Debt==. | AnyChecking==. | ln_r_CheckingAmount==.)



** Create a dummy equal to one for those who have all the info needed to appear in health regressions
gen InHealthReg =(ehltst~=. & r_MaxEITC_thou~=. & tage~=. & MidGenEN_Married~=. & MidGenEN_White~=. &  unemp_rate~=. & r_MaxWelf~=. & state_rate_wages~=. & minwage~=. & gdp~=. & any_w_a_extended~=. & rhcalyr~=. & tfipsst~=. & MissingHealthInfo==0)



** Create a dummy equal to one for those who have all the info needed to appear in debt regressions
gen InDebtReg =(AnyDebt~=. & r_MaxEITC_thou~=. & tage~=. & MidGenEN_Married~=. & MidGenEN_White~=. &  unemp_rate~=. & r_MaxWelf~=. & state_rate_wages~=. & minwage~=. & gdp~=. & any_w_a_extended~=. & rhcalyr~=. & tfipsst~=. & MissingDebtInfo==0)


** It may be useful to identify earliest observation with non-missing health info (and all the other
** info needed to be included in health regression)
gen swave_temp = swave
replace swave_temp = . if MissingHealthInfo==1 | InHealthReg~=1
sort UniquePersonID
by UniquePersonID: egen min_swave_Health = min(swave_temp)
drop swave_temp


** It may be useful to identify earliest observation with non-missing financial info (and all the other
** info needed to be included in financial regressions)
gen swave_temp = swave
replace swave_temp = . if MissingDebtInfo==1 | InDebtReg~=1
sort UniquePersonID
by UniquePersonID: egen min_swave_Debt = min(swave_temp)
drop swave_temp



gen Annualized_HH_inc = thtotinc*3
gen r_Annualized_HH_inc = real*Annualized_HH_inc


save OrganizedEmptyNesterParentLevelData, replace

